package com.yf.dxapp.module.app.controller.admin.appconfig;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import com.yf.dxapp.module.app.controller.admin.appconfig.vo.AppConfigPageReqVO;
import com.yf.dxapp.module.app.controller.admin.appconfig.vo.AppConfigRespVO;
import com.yf.dxapp.module.app.controller.admin.appconfig.vo.AppConfigSaveReqVO;
import com.yf.dxapp.module.app.dal.dataobject.appconfig.AppConfigDO;
import com.yf.dxapp.module.app.service.appconfig.AppConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - app配置信息")
@RestController
@RequestMapping("/dxapp/app-config")
@Validated
public class AppConfigController {

    @Resource
    private AppConfigService appConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建app配置信息")
    @PreAuthorize("@ss.hasPermission('t:app-config:create')")
    public CommonResult<Long> createAppConfig(@Valid @RequestBody AppConfigSaveReqVO createReqVO) {
        return success(appConfigService.createAppConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新app配置信息")
    @PreAuthorize("@ss.hasPermission('t:app-config:update')")
    public CommonResult<Boolean> updateAppConfig(@Valid @RequestBody AppConfigSaveReqVO updateReqVO) {
        appConfigService.updateAppConfig(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除app配置信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('t:app-config:delete')")
    public CommonResult<Boolean> deleteAppConfig(@RequestParam("id") Long id) {
        appConfigService.deleteAppConfig(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得app配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('t:app-config:query')")
    public CommonResult<AppConfigRespVO> getAppConfig(@RequestParam("id") Long id) {
        AppConfigDO appConfig = appConfigService.getAppConfig(id);
        return success(BeanUtils.toBean(appConfig, AppConfigRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得app配置信息分页")
    @PreAuthorize("@ss.hasPermission('t:app-config:query')")
    public CommonResult<PageResult<AppConfigRespVO>> getAppConfigPage(@Valid AppConfigPageReqVO pageReqVO) {
        PageResult<AppConfigDO> pageResult = appConfigService.getAppConfigPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AppConfigRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出app配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('t:app-config:export')")
    
    public void exportAppConfigExcel(@Valid AppConfigPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AppConfigDO> list = appConfigService.getAppConfigPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "app配置信息.xls", "数据", AppConfigRespVO.class,
                        BeanUtils.toBean(list, AppConfigRespVO.class));
    }

}